package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.small.util.ResponseResultDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.CustomerAddress;
import com.stylefeng.guns.modular.system.service.ICustomerAddressService;

import java.util.HashMap;
import java.util.Map;


/**
 * @apiDefine CustomerAddressNotFoundError
 *
 * @apiError CustomerAddressNotFound The id of the CustomerAddress was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "CustomerAddressNotFound"
 *     }
 */
@Controller
@RequestMapping("/customerAddress")
public class CustomerAddressController extends BaseController {

    private String PREFIX = "/system/customerAddress/";

    @Autowired
    private ICustomerAddressService customerAddressService;

    /**
     * 跳转到客户服务地址首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "customerAddress.html";
    }

    /**
     * 跳转到添加客户服务地址
     */
    @RequestMapping("/customerAddress_add")
    public String customerAddressAdd() {
        return PREFIX + "customerAddress_add.html";
    }

    /**
     * 跳转到修改客户服务地址
     */
    @RequestMapping("/customerAddress_update/{customerAddressId}")
    public String customerAddressUpdate(@PathVariable Integer customerAddressId, Model model) {
        CustomerAddress customerAddress = customerAddressService.selectById(customerAddressId);
        model.addAttribute("item",customerAddress);
        LogObjectHolder.me().set(customerAddress);
        return PREFIX + "customerAddress_edit.html";
    }

    /**
     * @api {get} /customerAddress/list  获取客户服务地址列表
     * @apiName customerAddressList
     * @apiGroup CustomerAddress
     *
     * @apiSuccessExample {json} Success-Response:
     *      HTTP/1.1 200 OK
     *   [
     *        {
     * 		"address":"四川省-成都市-武侯区-红牌楼街道111号",
     * 		"addressId":1,
     * 		"consumerId":1,
     * 		"lat":30.6390936076,
     * 		"lng":104.0501403809,
     * 		"name":"张先生",
     * 		"phone":"13468544566",
     * 		"sex":1,
     * 		"type":1
     *    },
     *    {
     * 		"address":"四川省-成都市-武侯区-红牌楼街道111号",
     * 		"addressId":2,
     * 		"consumerId":2,
     * 		"lat":30.6390936076,
     * 		"lng":104.0501403809,
     * 		"name":"李先生",
     * 		"phone":"13468544511",
     * 		"sex":1,
     * 		"type":0
     *    }
     * ]
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return customerAddressService.selectList(null);
    }

    /**
     * @api {POST} /customerAddress/add  新增客户服务地址
     * @apiName  addCustomerAddress
     * @apiGroup CustomerAddress
     *
     * @apiParam {Object} CustomerAddress 传入的参数与CustomerAddress对象的字段对应(主键递增).
     *
     * @apiSuccessExample {json} Success-Response:
     *      HTTP/1.1 200 OK
     *    {
     * 		"message":"操作成功",
     * 	    "code":"200"
     *    }
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CustomerAddress customerAddress) {
        customerAddressService.insert(customerAddress);
        return SUCCESS_TIP;
    }

    /**
     * @api {POST} /customerAddress/delete 删除客户服务地址
     * @apiName  deleteCustomerAddress
     * @apiGroup CustomerAddress
     *
     * @apiParam {Number} customerAddressId 消费者地址Id.
     *
     * @apiSuccessExample {json} Success-Response:
     *      HTTP/1.1 200 OK
     *    {
     * 		"message":"操作成功",
     * 	    "code":"200"
     *    }
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer customerAddressId) {
        customerAddressService.deleteById(customerAddressId);
        return SUCCESS_TIP;
    }

    /**
     * @api {POST} /customerAddress/update  修改客户服务地址
     * @apiName  updateCustomerAddress
     * @apiGroup CustomerAddress
     *
     * @apiParam {Object} CustomerAddress 传入的参数与CustomerAddress对象的字段对应+addressId(标识主键id).
     *
     * @apiSuccessExample {json} Success-Response:
     *      HTTP/1.1 200 OK
     *    {
     * 		"message":"操作成功",
     * 	    "code":"200"
     *    }
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CustomerAddress customerAddress) {
        customerAddressService.updateById(customerAddress);
        return SUCCESS_TIP;
    }

    /**
     * 客户服务地址详情
     */
    @RequestMapping(value = "/detail/{customerAddressId}")
    @ResponseBody
    public Object detail(@PathVariable("customerAddressId") Integer customerAddressId) {
        return customerAddressService.selectById(customerAddressId);
    }


    @GetMapping(value = "/list/{consumerId}")
    @ResponseBody
    public Object list(@PathVariable("consumerId") Integer consumerId) {
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("consumerId",consumerId);
        return customerAddressService.selectByMap(columnMap);
    }


    @PostMapping(value = "/address/add")
    @ResponseBody
    public Object addressAdd(@RequestBody CustomerAddress customerAddress) {
        customerAddressService.insert(customerAddress);
        return SUCCESS_TIP;
    }

    @DeleteMapping(value = "/address/delete/{customerAddressId}")
    @ResponseBody
    public Object deleteAddress(@PathVariable("customerAddressId") Integer customerAddressId) {
        customerAddressService.deleteById(customerAddressId);
        return SUCCESS_TIP;
    }

    @PutMapping(value = "/address/update")
    @ResponseBody
    public Object updateAddress(@RequestBody CustomerAddress customerAddress) {
        customerAddressService.updateById(customerAddress);
        return SUCCESS_TIP;
    }

    /**
     *查询距离和 价格
     * @param alat 技师坐标
     * @param along
     * @param clat  客户坐标
     * @param clong
     * @return
     */
    @RequestMapping(value = "/chekCarPrice")
    @ResponseBody
    public ResponseResultDTO chekCarPrice(String alat, String along, String clat, String clong) {
        return customerAddressService.chekCarPrice(alat,along,clat,clong);
    }


}
